package com.my.zidingyikongjian.view.three;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.RegionIterator;
import android.view.View;

/**
 * 项目名称：Set函数的使用
 * 创建人：mwb
 * 创建时间：2018/7/12 11:26
 */

public class MySetView extends View {
    private int flag;

    public MySetView(Context context, int flag) {
        super(context);
        this.flag = flag;
    }

    @Override
    protected void onDraw(Canvas canvas) {

        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.FILL); // 填充
        paint.setStrokeWidth(2);

        if (flag == 1) {
            Region region = new Region(10, 10, 100, 100);
//        region.set(100, 100, 200, 200);
            drawRegion(canvas, region, paint);

            paint.setColor(Color.RED);
            region.set(100, 100, 200, 200);
            drawRegion(canvas, region, paint);
        } else if (flag == 2) {
            paint.setStyle(Paint.Style.STROKE); // 填充
            // 创造一个椭圆的路径
            Path ovalPath = new Path();
            RectF rect = new RectF(50, 50, 200, 500);
            ovalPath.addOval(rect, Path.Direction.CCW);
            // SetPath时，传入一个比椭圆区域更小的矩形，让他们交集
            Region region = new Region();
            region.setPath(ovalPath, new Region(50, 50, 200, 200));
            // 画出路径
            drawRegion(canvas, region, paint);
        } else if (flag == 3) {
            // 构造两个矩形
            Rect rect1 = new Rect(100, 100, 400, 200);
            Rect rect2 = new Rect(200, 0, 300, 300);

            // 构造第二套矩形
            Rect rect3 = new Rect(500, 100, 800, 200);
            Rect rect4 = new Rect(600, 0, 700, 300);

            // 构造第三套矩形
            Rect rect5 = new Rect(100, 500, 400, 600);
            Rect rect6 = new Rect(200, 400, 300, 700);


            // 构造第四套矩形
            Rect rect7 = new Rect(500, 500, 800, 600);
            Rect rect8 = new Rect(600, 400, 700, 700);

            // 构造第五套矩形
            Rect rect9 = new Rect(100, 900, 400, 1000);
            Rect rect10 = new Rect(200, 800, 300, 1100);

            // 构造第六套矩形
            Rect rect11 = new Rect(500, 900, 800, 1000);
            Rect rect12 = new Rect(600, 800, 700, 1100);

            // 构造一个画笔,画出矩形轮廓
            Paint paint1 = new Paint();
            paint1.setColor(Color.RED);
            paint1.setStyle(Paint.Style.STROKE);
            paint1.setStrokeWidth(2);

            canvas.drawRect(rect1, paint1);
            canvas.drawRect(rect2, paint1);

            canvas.drawRect(rect3, paint1);
            canvas.drawRect(rect4, paint1);

            canvas.drawRect(rect5, paint1);
            canvas.drawRect(rect6, paint1);

            canvas.drawRect(rect7, paint1);
            canvas.drawRect(rect8, paint1);

            canvas.drawRect(rect9, paint1);
            canvas.drawRect(rect10, paint1);

            canvas.drawRect(rect11, paint1);
            canvas.drawRect(rect12, paint1);

            // 构造两个Region
            Region region = new Region(rect1);
            Region region2 = new Region(rect2);

            Region region3 = new Region(rect3);
            Region region4 = new Region(rect4);

            Region region5 = new Region(rect5);
            Region region6 = new Region(rect6);

            Region region7 = new Region(rect7);
            Region region8 = new Region(rect8);

            Region region9 = new Region(rect9);
            Region region10 = new Region(rect10);

            Region region11 = new Region(rect11);
            Region region12 = new Region(rect12);

            // 取俩个区域交集
            region.op(region2, Region.Op.INTERSECT); // 交集
            region3.op(region4, Region.Op.DIFFERENCE);// 补集
            region5.op(region6, Region.Op.REPLACE);// 补集
            region7.op(region8, Region.Op.REVERSE_DIFFERENCE);// 翻转补集
            region9.op(region10, Region.Op.UNION);// 并集
            region11.op(region12, Region.Op.XOR);// 并集

            //在构造一个画笔，填充Region操作结果
            Paint paint_fill = new Paint();
            paint_fill.setColor(Color.GREEN);
            paint_fill.setStyle(Paint.Style.FILL);
            drawRegion(canvas, region, paint_fill);
            drawRegion(canvas, region3, paint_fill);
            drawRegion(canvas, region5, paint_fill);
            drawRegion(canvas, region7, paint_fill);
            drawRegion(canvas, region9, paint_fill);
            drawRegion(canvas, region11, paint_fill);
        } else if (flag == 4) { // canvas变换与操作
            Paint paint4 = new Paint();
            paint4.setStyle(Paint.Style.STROKE); // 描边
            paint4.setColor(Color.GREEN);
            paint4.setStrokeWidth(5);

            RectF rectF = new RectF(0, 0, 400, 220);
            canvas.drawRect(rectF, paint4);

            // canvas x轴y轴平移100单位
            paint4.setColor(Color.RED);
            canvas.translate(100, 100);
            canvas.drawRect(rectF, paint4);
        }
    }

    private void drawRegion(Canvas canvas, Region region, Paint paint) {
        RegionIterator iterator = new RegionIterator(region);
        Rect r = new Rect();

        while (iterator.next(r)) {
            canvas.drawRect(r, paint);
        }
    }
}
